<template>
  <div class="home-container" ref="container" @wheel="handleWheel">
    <ul
      class="carousel-container"
      :style="{
        marginTop,
      }"
      @transitionend="handleTransitionEnd"
    >
      <li>
        <CarouselItemOne />
      </li>
      <li>
        <CarouselItemTwo />
      </li>
      <li>
        <CarouselItemThree />
      </li>
    </ul>
    <div v-show="index >= 1" @click="switchTo(index - 1)" class="icon icon-up">
      <IndexIcon type="arrowUp" />
    </div>
    <div v-show="index < 2" @click="switchTo(index + 1)" class="icon icon-down">
      <IndexIcon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        v-for="(item, i) in items"
        :key="item.id"
        :class="{ active: i === index }"
        @click="switchTo(i)"
      >
        {{ item.li }}
      </li>
    </ul>
  </div>
</template>

<style scoped lang="less">
@import "~@/style/mixin.less";
@import "~@/style/var.less";
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;

  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
}
.carousel-container {
  width: 100%;
  height: 100%;
  transition: 500ms;
  li {
    width: 100%;
    height: 100%;
  }
}
@keyframes jump-up {
  0% {
    transform: translate(-50%, 5px);
  }
  50% {
    transform: translate(-50%, -5px);
  }
  100% {
    transform: translate(-50%, 5px);
  }
}
@keyframes jump-down {
  0% {
    transform: translate(-50%, 5px);
  }
  50% {
    transform: translate(-50%, -5px);
  }
  100% {
    transform: translate(-50%, 5px);
  }
}
.icon {
  .self-center();
  font-size: 30px;
  color: @gray;
  cursor: pointer;
  transform: translateX(-50%);
}
.icon-up {
  top: 20px;
  animation: jump-up 2s infinite;
}
.icon-down {
  top: auto;
  bottom: 20px;
  animation: jump-down 2s infinite;
}
.indicator {
  .self-center();
  right: 20px;
  left: auto;
  transform: translateY(-50%);
  li {
    width: 7px;
    height: 7px;
    background: @words;
    cursor: pointer;
    margin-bottom: 10px;
    border-radius: 50%;
    border: 1px solid #fff;
    box-sizing: border-box;
  }
}
</style>

<script>
import CarouselItemOne from "./CarouselItemOne";
import CarouselItemTwo from "./CarouselItemTwo";
import CarouselItemThree from "./CarouselItemThree";
import IndexIcon from "@/components/Icon/IndexIcon.vue";

export default {
  components: {
    CarouselItemOne,
    CarouselItemTwo,
    CarouselItemThree,
    IndexIcon,
  },
  data() {
    return {
      banners: [],
      index: 0,
      containerHeight: 0,
      items: [{ li: "" }, { li: "" }, { li: "" }],
      switching: false,
    };
  },

  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.handleResize);
  },
  destroyed() {
    window.removeEventListener("resize", this.handleResize);
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
  },
  methods: {
    // 切换轮播图
    switchTo(i) {
      this.index = i;
    },
    handleWheel(e) {
      if (this.switching) {
        return;
      }
      if (e.deltaY < -5 && this.index > 0) {
        // 往上滚动
        this.switching = true;
        this.index--;
      } else if (e.deltaY > 5 && this.index < 2) {
        // 往下滚动
        this.switching = true;
        this.index++;
      }
    },
    handleTransitionEnd() {
      this.switching = false;
    },
    handleResize() {
      this.containerHeight = this.$refs.container.clientHeight;
    },
  },
};
</script>